package com.xpresso.utils.image;

import java.io.File;
import java.util.Iterator;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

public class JpgExtractor implements ExifExtractor {

	public String getExifInfo(String path) {
		StringBuffer sb = new StringBuffer();
		File jpegFile = new File(path);
		Metadata metadata = null;
		try {
			metadata = JpegMetadataReader.readMetadata(jpegFile);
		} catch (JpegProcessingException e) {
			// TODO throw ImageException
			e.printStackTrace();
		}
		// iterate through metadata directories 
		Iterator directories = metadata.getDirectoryIterator(); 
		while (directories.hasNext()) { 
			Directory directory = (Directory)directories.next(); 
			// iterate through tags and print to System.out  
			Iterator tags = directory.getTagIterator(); 
			while (tags.hasNext()) { 
				Tag tag = (Tag)tags.next(); 
				sb.append(tag.toString() + "\n");		
			}
		}
		
		return sb.toString();
	}

}
